
<template>
    <div class="iframe-cont">
        <div class="content">
            <div class="title">{{$route.meta.title}}
                <span v-if="id" style="font-size: 13px">（当前批次号：
                    <el-tag  type="success" closable @close="removeBatchNo">{{$route.query.batchNo}}</el-tag>）
                </span></div>
            <el-divider></el-divider>
            <!--     筛选区       -->
            <div class="f f-a-c">
                <div class="search-label">
                    <span class="label-title">使用状态</span>
                    <el-select
                        v-model="queryParams.codeStatus"
                        @change=""
                        clearable
                        size="small"
                        placeholder="请选择"
                        @change="handleQuery"
                    >
                        <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </div>
                <el-input
                    class="search-group mar-left-20"
                    style="width: 280px"
                    placeholder="输入二维码编号或设备编号"
                    size="small"
                    clearable
                    v-model="queryParams.keyword"
                    @keydown.native.enter="handleQuery"
                >
                    <el-button slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
                </el-input>
                <div class="f-1"></div>
            </div>
            <el-table class="table th-bg"
                      :data="dataList"
                      v-loading="loading"
                      element-loading-text="加载中"
                      element-loading-spinner="el-icon-loading"
            >
                <el-table-column label="序号" prop="name" width="60" fixed>
                    <template slot-scope="scope">
                        <span>{{(queryParams.page - 1) * queryParams.limit + scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="二维码编号" prop="qrCode"></el-table-column>
                <el-table-column label="设备编号" prop="macCode"></el-table-column>
                <el-table-column label="使用状态" prop="codeStatus">
                    <template slot-scope="{row}">
                        <span v-if="row.codeStatus===1">已使用</span>
                        <span v-else-if="row.codeStatus===2" style="color: red;">已作废</span>
                        <span v-else-if="row.codeStatus===0">未使用</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150px" align="center" fixed="right">
                    <template slot-scope="{row}">
                        <el-button v-if="row.codeStatus===1" type="text" @click="seeQrcode(row)">查看</el-button>
                        <el-button v-else-if="row.codeStatus===0" type="text" @click="readyBind(row)">绑定</el-button>
                        <span v-else>--</span>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.page"
                :limit.sync="queryParams.limit"
                @pagination="getList"
            />
        </div>

<!--        生成二维码-->
        <el-dialog
            :append-to-body="true"
            title="二维码绑定"
            :visible.sync="addCodeDialog"
            width="440px"
            center
        >
            <el-input class="form-comp-item" type="text" v-model="form.macCode" placeholder="输入需要绑定的二维码的设备编号"></el-input>
            <div slot="footer">
                <el-button @click="addCodeDialog=false">取 消</el-button>
                <span style="display: inline-block;width: 20px;"></span>
                <el-button type="primary" @click="bindCode">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import SearchMacType from "@/components/SearchCondition/SearchMacType";
import {Loading, Message} from "element-ui";
import ParamsSet from "@/components/ParamsSet";
import SearchDeviceClass from "@/components/SearchCondition/SearchDeviceClass";
import {genQrcode} from "@/api/common";
import {previewImg} from "@/utils/previewImg";
export default {
    name: "index",
    components: {SearchDeviceClass, ParamsSet, SearchMacType},
    data(){
        return{
            dataList: [1],
            loading:false,//加载中
            addCodeDialog: false,
            total:10,
            queryParams: {
                limit: this.$tmp.PAGE_SIZES[0],
                page: 1,
                keyword: null,
                batchId: null, // 批次
                codeStatus: null, // 二维码状态(0-未使用,1-已使用,2-作废)
            },
            options: [
                { label: '未使用', value: 0 },
                { label: '已使用', value: 1 },
                { label: '已作废', value: 2 },
            ],
            form: {
                macCode: null,
                id: null,
            },
            id: null, // 批次生成传过来的批次id
        }
    },
    created() {
        if(this.$route.query.id) {
            this.id = this.$route.query.id
            this.batchNo = this.$route.query.batchNo
        }
        this.getList();
    },
    methods:{
        handleQuery() {
            this.queryParams.page = 1
            this.getList()
        },
        removeBatchNo() {
            this.id = null
            this.batchNo = null
            this.getList()
        },
        seeQrcode(row) {
            let loadingInstance1 = Loading.service({ fullscreen: true });
            this.$http({
                url:'/api/qrcode/see',
                method: 'get',
                data: { id: row.id },
                responseType:'blob'
            }).then(res=>{
                let url = URL.createObjectURL(new Blob([res]))
                previewImg(url)
            }).finally(() => {
                loadingInstance1.close()
            })
        },
        readyBind(row) {
            this.form.id = row.id
            this.addCodeDialog = true
        },
        bindCode(row) {
            this.$http({
                url:'/api/qrcode/bind',
                data: this.form,
            }).then(res=>{
                Message.success('绑定成功！')
                this.addCodeDialog = false
                this.getList()
            })
        },
        // 获取数据
        getList() {
            this.loading = true;
            this.queryParams.batchId = this.id
            this.$http({
                url:'/api/qrcode/page',
                method: 'get',
                data: this.queryParams,
            }).then(res=>{
                this.dataList = res.data;
                this.total = res.count;
            }).finally(() => {
                this.loading = false
            })
        },
    }
}
</script>

<style lang="scss" scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
::v-deep input[type="number"]{
    -moz-appearance: textfield;
}
::v-deep .el-form-item__content {
    margin-left: 10px;
}
.form-item {
    width: 400px;
}

.table {
    margin-top: 20px;
}
::v-deep .el-table__row {
    cursor: pointer;
}
</style>
